import { getDate } from "./time.mjs";
import tMysql from "mysql";
const { createPool } = tMysql;
var pool = createPool({
	host: "127.0.0.1",
	port: "3306",
	user: "arduino_test",
	password: "666",
	database: "arduino_test",
});
/**
 * @param {String} sql
 * - 需要执行的 sql 语句
 * @param {Function} callback  (data, fields)回调函数
 */
function query(sql, callback) {
	pool.getConnection((err, conn) => {
		if (err) {
			console.log("ERROR - [pool.getConnection] \n", err.sqlMessage);
		} else {
			conn.query(sql, (qerr, data, fields) => {
				conn.release();
				if (qerr) {
					console.log("ERROR - [query] \n", qerr.sqlMessage);
				} else {
					callback(data, fields);
					// process.exit(0);
				}
			});
		}
	});
}
/**
 * @param {String} sql -
 * - 需要执行的 sql 语句
 * - 可使用 `?` 作为占位符
 * @param {Array} [sqlParams]
 * - 数组元素将依次代入 `?`
 * @param {Function} callback  (data, fields)回调函数
 */
function insert(sql, sqlParams, callback) {
	pool.getConnection((err, conn) => {
		if (err) {
			console.log("ERROR - [pool.getConnection] \n", err.sqlMessage);
		} else {
			conn.query(sql, sqlParams, (qerr, result, fields) => {
				conn.release();
				if (qerr) {
					console.log("ERROR - [insert] \n", qerr.sqlMessage);
				} else {
					callback(result, fields);
					console.log("--------------------insert----------------------");
					console.log("INSERT affectedRows: ", result.affectedRows);
					console.log("------------------------------------------------");
					// process.exit(0);
				}
			});
		}
	});
}
/**
 * @param {String} sql -
 * - 需要执行的 sql 语句
 * - 可使用 `?` 作为占位符
 * @param {Array} [sqlParams]
 * - 数组元素将依次代入 `?`
 * @param {Function} callback  (data, fields)回调函数
 */
function update(sql, sqlParams, callback) {
	pool.getConnection((err, conn) => {
		if (err) {
			console.log("ERROR - [pool.getConnection] \n", err.sqlMessage);
		} else {
			conn.query(sql, sqlParams, (qerr, result, fields) => {
				conn.release();
				if (qerr) {
					console.log("ERROR - [update] \n", qerr.sqlMessage);
				} else {
					callback(result, fields);
					console.log("--------------------UPDATE----------------------");
					console.log("UPDATE affectedRows: ", result.affectedRows);
					console.log("------------------------------------------------");
					// process.exit(0);
				}
			});
		}
	});
}
/**
 * @param {String} sql -
 * - 需要执行的 sql 语句
 * - 可使用 `?` 作为占位符
 * @param {Array} [sqlParams]
 * - 数组元素将依次代入 `?`
 * @param {Function} callback  (data, fields)回调函数
 */
function del(sql, sqlParams, callback) {
	pool.getConnection((err, conn) => {
		if (err) {
			console.log("ERROR - [pool.getConnection] \n", err.sqlMessage);
		} else {
			conn.query(sql, sqlParams, (qerr, result, fields) => {
				conn.release();
				if (qerr) {
					console.log("ERROR - [delete] \n", qerr.sqlMessage);
				} else {
					if (result.affectedRows > 0) {
						console.log("--------------------DELETE----------------------");
						console.log("DELETE affectedRows", result.affectedRows);
						console.log("------------------------------------------------");
					} else {
						console.log(
							"找不到删除对象:[id:  " +
								sqlParams[0] +
								" ,time: " +
								getDate(true) +
								" ]\n"
						);
					}
					callback(result, fields);
					// process.exit(0);
				}
			});
		}
	});
}
export default { query: query, del: del, insert: insert, update: update };
